﻿#region copyright
//Copyright 2013 Ramon Ordiales Plaza
//
//This file is part of TrayFastMsg.
//
//    TrayFastMsg is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    TrayFastMsg is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with FastMessage.  If not, see <http://www.gnu.org/licenses/>.
//
#endregion
using FastMessage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrayFastMsg
{
    public class MsgManager
    {
        
        public List<FastMessage.Message> msgData { get; private set; }
        public event EventHandler NewMessages;
        public SynchronizationContext scontext;
        public FastMessageReader ReadTask;
        public bool NoMsg { get { return msgData.Count == 0; } }
        private readonly NotifyIcon notifyIcon;
        private System.Windows.Forms.Timer tm;
        public MsgManager(NotifyIcon notifyIcon)
        {
            msgData = new List<FastMessage.Message>();
            grayicon = ROPControls.HelperIcon.PngIconFromImage(TrayFastMsg.Properties.Resources.alertgray);
            blueicon = ROPControls.HelperIcon.PngIconFromImage(TrayFastMsg.Properties.Resources.alert);
            this.notifyIcon = notifyIcon;
            SetIcon();
            scontext = SynchronizationContext.Current;
            ReadTask = new FastMessageReader();
            tm = new System.Windows.Forms.Timer();
            tm.Interval = 500;
            tm.Tick += tm_Tick;
            tm.Start();
        }
        private object locktick = new object();
        void tm_Tick(object sender, EventArgs e)
        {
            if (!ReadTask.HayMensajes) return;
            lock (locktick)
            {
                while (ReadTask.HayMensajes)
                {
                    msgData.Add(ReadTask.GetMensaje());
                }
                SetIcon();
                this.notifyIcon.ShowBalloonTip(1000, "Mensaje Recibido", msgData.Count().ToString() + " Mensajes recibidos", ToolTipIcon.Info);
                try
                {
                    if (NewMessages != null) NewMessages(this, new EventArgs());
                }catch(Exception ex){
                    Debug.Print(ex.Message);
                }
            }
            
        }
        private Icon grayicon;
        private Icon blueicon;
        
        public void SetIcon()
        {
            notifyIcon.Icon = (NoMsg) ? grayicon : blueicon;
            notifyIcon.Text = (NoMsg) ? "Ningún Mensaje urgente" : string.Format("{0} Mensajes", msgData.Count());
        }    
        
        
    }
}
